﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI
{
    public partial class OpenFromServer : Form
    {
        public OpenFromServer()
        {
            InitializeComponent();
        }

        public OpenFromServer(List<string> list)
            : this()
        {
            foreach (string item in list)
                lb.Items.Add(item);
        }

        private Guid ID;

        private void bApply_Click(object sender, EventArgs e)
        {
            string item = lb.SelectedItem.ToString();
            ID = new Guid(item.Split('|')[0]);
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            bApply.Enabled = lb.SelectedIndex >= 0;
        }

        public static TasksModel.TaskLibrary Show(TasksModel.TaskLibrary library)
        {
            TasksUI.Helpers.WsHelper ws = new TasksUI.Helpers.WsHelper();
            ws.Url = ((TasksModel.WebServiceSynchSettings)library.SynchSettings).Url;
            List<string> list = ws.GetList();
            using (OpenFromServer f = new OpenFromServer(list))
            {
                if (f.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                {
                    return ws.Get(f.ID);
                }
                else
                    return null;
            }
        }
    }
}
